#!/bin/bash

base_dir=$(pwd)
# 测试轮次，默认1次，按需修改
source $base_dir/user_config

echo "[INFO] 关闭交换区分"
swapoff -a
echo "[INFO] 关闭防火墙 firewalld"
systemctl stop firewalld
systemctl disable firewalld

echo "[INFO] 安装编译和运行依赖的组件：gcc make libaio libaio-devel perl zlib-devel gcc-gfortran expect tar"
yum install -y gcc make libaio libaio-devel perl zlib-devel gcc-gfortran expect tar  2>&1 > /dev/null
if [ $? -eq 0 ]; then
    echo "[INFO] yum-安装成功"
else
    echo "[ERROR] yum-安装失败，退出测试 hackbench"
    exit 1
fi

cd $base_dir
echo "[INFO] 编译 hackbench"
gcc -o hackbench hackbench.c -lpthread  2>&1 > /dev/null

sync
echo 3 > /proc/sys/vm/drop_caches
sleep 3s

# 在源码路径下运行如下命令进行测试
echo "[INFO] 开始测试 hackbench"

if [ -z "${log_file}" ]; then
    cur_time=`date +"%Y%m%d_%H%M%S"`
    log_dir="/home/log_${cur_time}_`uname -r`"
    mkdir -p "${log_dir}"
    declare log_file="${log_dir}"/hackbench
fi

for k in $(seq $hackbench_run_counts);
do
    # 用pipe方式分别以40/80/120进程数量运行500000次测调度耗时
    echo "[INFO] ---- 第 $k 轮 进程调度耗时测试 开始测试 ----"
    for i in 1 2 3;do
        echo "-------- process num=$i ----------"
        $base_dir/hackbench -pipe $i process 500000
    done >> "${log_file}_${k}"

    # 用pipe方式分别以40/80/120线程数量运行500000次测调度耗时
    echo "[INFO] ---- 第 $k 轮 线程调度耗时测试 开始测试 ----"
    for i in 1 2 3;do
        echo "-------- thread num=$i ---------"
        $base_dir/hackbench -pipe $i thread 500000
    done >> "${log_file}_${k}"

    echo -e "\033[32m [INFO] `date` hackbench 运行完成，结果存放在 ${log_file}_${k} \033[0m"
done
